﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda IBaseAppPanel</summary>
    public class IBaseAppPanel : System.Windows.Forms.UserControl
    {
        public const int APP_PANEL_ID_NONE = 0;
        public const int APP_PANEL_ID_FILE_EXPLORER = 1;

        public int ID; // ID panela
        public int TabPosition; // Pozicia zalozky panela

        protected IParamList FParams; // Parametre panela
        protected int FCMDGroupID; // ID skupiny prikazov
        protected bool FDisabledActivate; // Priznak zakazanej aktivacie panela
        protected string FCustomTabName; // Vlastny nazov zalozky

        /// <summary>Property: TabName</summary>
        public virtual string TabName
        {
            get
            {
                return "";
            }
            set
            {
                FCustomTabName = value; // Ulozim
                FParams.SetParam("CTN", value); // Ulozim do nastavenia
                UpdateTabPageName();
            }
        }
        /// <summary>Property: CMDGroupID</summary>
        public virtual int CMDGroupID
        {
            get
            {
                return FCMDGroupID;
            }
        }
        /// <summary>Property: CanClose</summary>
        public virtual bool CanClose
        {
            get
            {
                return true;
            }
        }
        /// <summary>Property: CustomTabName</summary>
        public string CustomTabName
        {
            get
            {
                return FCustomTabName;
            }
        }
        /// <summary>Property: InitString</summary>
        public virtual string InitString
        {
            get
            {
                return "";
            }
        }

        /// <summary>Konstruktor</summary>
        public IBaseAppPanel()
        {
            ID = APP_PANEL_ID_NONE;
            TabPosition = -1;
            FParams = new IParamList();
            FCMDGroupID = ICmd.CMD_GROUP_COMMON;
            FDisabledActivate = false;
            FCustomTabName = "";
        }
        /// <summary>Dispose metoda</summary>
        /// <param name="disposing">parameter</param>
        protected override void Dispose(bool disposing)
        {
            FParams = (IParamList)ICore.DisposeObject(FParams); // Zrusim parametre
            base.Dispose(disposing); // Volam predchodcu
        }

        /// <summary>Inicializacia objektu</summary>
        /// <param name="pInitStr">inicializacne data</param>
        /// <returns>true / false</returns>
        public virtual bool Init(string pInitStr = "")
        {
            return true; // Vsetko OK
        }
        /// <summary>Uzatvorenie panela</summary>
        public virtual void Close()
        {
        }
        /// <summary>Nacitanie nastavenia</summary>
        public virtual void LoadSettings()
        {
            if (TabPosition == -1) return; // Neinicializovana pozicia?
            FParams.Deserialize(LoadSettings(ID, TabPosition), false); // Nacitam a deserializujem
            FCustomTabName = FParams.GetParam("CTN", ""); // Nacitam nazov zalozky
        }
        /// <summary>Ulozenie nastavenia</summary>
        public virtual void SaveSettings()
        {
            if (TabPosition == -1) return; // Neinicializovana pozicia?
            FParams.GetParam("CTN", FCustomTabName); // Ulozim nazov zalozky
            SaveSettings(ID, TabPosition, FParams.Serialize(false)); // Serializujem a volam ulozenie
        }
        /// <summary>Vymazanie nastavenia</summary>
        public virtual void DeleteSettings()
        {
            if (TabPosition == -1) return; // Neinicializovana pozicia?
            DeleteSettings(ID, TabPosition); // Volam vymazanie
        }
        /// <summary>Vytvorenie menu aplikacneho panela</summary>
        /// <param name="pParent">rodicovska kolekcia</param>
        public virtual void CreatePanelMenu(System.Windows.Forms.ToolStripItemCollection pParent)
        {
        }
        /// <summary>Vykonanie prikazu</summary>
        /// <param name="pCMD_ID">ID prikazu</param>
        /// <param name="pAPP_TARGET">aplikacia</param>
        /// <param name="pPANEL_TARGET">panel</param>
        /// <returns>true / false</returns>
        public virtual bool ExecCMD(int pCMD_ID, int pAPP_TARGET, int pPANEL_TARGET)
        {
            return false; // Prikaz nebol vykonany
        }
        /// <summary>Aktualizacia nazvu zalozky</summary>
        public virtual void UpdateTabPageName()
        {
            IApp.AppForm.SetTabPageName(this, TabName); // Volam nastavenie
        }
        /// <summary>Aktualizacia objektu po zmene nastavenia</summary>
        public virtual void UpdateAfterChangeSettings()
        {
        }
        /// <summary>Aktualizacia objektu po zmene vizualnej temy</summary>
        public virtual void UpdateAfterChangeVisualTheme()
        {

        }
        /// <summary>Aktivacia panela</summary>
        public virtual void ActivatePanel()
        {
        }
        /// <summary>Duplikovanie panela</summary>
        public virtual void DuplicatePanel()
        {
        }

        /// <summary>Nacitanie parametrov z nastavenia</summary>
        /// <param name="pID">ID panela</param>
        /// <param name="pTabPosition">pozicia</param>
        /// <returns>serializovany obsah</returns>
        public static string LoadSettings(int pID, int pTabPosition)
        {
            return ICore.Base64Decode(IApp.Settings.App_PanelSettings.GetParam(string.Format("{0}_{1}", pID, pTabPosition), "")); // Ziskam serializovany obsah a vratim ho
        }
        /// <summary>Ulozenie parametrov do nastavenia</summary>
        /// <param name="pID">ID panela</param>
        /// <param name="pTabPosition">pozicia</param>
        /// <param name="pSerializedContent">serializovany obsah</param>
        public static void SaveSettings(int pID, int pTabPosition, string pSerializedContent)
        {
            IApp.Settings.App_PanelSettings.SetParam(string.Format("{0}_{1}", pID, pTabPosition), ICore.Base64Encode(pSerializedContent)); // Ulozim do nastavenia
        }
        /// <summary>Vymazanie parametrov z nastavenia</summary>
        /// <param name="pID">ID panela</param>
        /// <param name="pTabPosition">pozicia</param>
        public static void DeleteSettings(int pID, int pTabPosition)
        {
            IApp.Settings.App_PanelSettings.Delete(string.Format("{0}_{1}", pID, pTabPosition)); // Vymazem z parametrov
        }
        /// <summary>Presun nastaveni</summary>
        /// <param name="pSource"></param>
        /// <param name="pDest"></param>
        public static void DeleteSettings(int pTabPosition)
        {
            string N = string.Format("{0}_{1}", IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER, pTabPosition); // Mazem APP_PANEL_ID_FILE_EXPLORER
            if (IApp.Settings.App_PanelSettings.Exists(N)) IApp.Settings.App_PanelSettings.Delete(N); 
        }
        /// <summary>Presun kompletneho nastavenia z jednej pozicie na druhu</summary>
        /// <param name="pSourcePosition">zdrojova pozicia</param>
        /// <param name="pDestPosition">cielova pozicia</param>
        public static void MoveSettings(int pSourcePosition, int pDestPosition)
        {
            DeleteSettings(pDestPosition); // Vymazem cielovy obsah

            string N1 = string.Format("{0}_{1}", IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER, pSourcePosition); // Presuvam APP_PANEL_ID_FILE_EXPLORER
            string N2 = string.Format("{0}_{1}", IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER, pDestPosition);
            if (IApp.Settings.App_PanelSettings.Exists(N1)) IApp.Settings.App_PanelSettings.SetParam(N2, IApp.Settings.App_PanelSettings.GetParam(N1, ""));
        }
        /// <summary>Klonovanie nastavenia</summary>
        /// <param name="pSourcePosition">zdrojova pozicia</param>
        /// <param name="pDestPosition">cielova pozicia</param>
        public static void CloneSettings(int pSourcePosition, int pDestPosition)
        {
            string N1 = string.Format("{0}_{1}", IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER, pSourcePosition); // Presuvam APP_PANEL_ID_FILE_EXPLORER
            string N2 = string.Format("{0}_{1}", IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER, pDestPosition);
            if (IApp.Settings.App_PanelSettings.Exists(N1)) IApp.Settings.App_PanelSettings.SetParam(N2, IApp.Settings.App_PanelSettings.GetParam(N1, ""));
        }

        /// <summary>Vytvorenie objektu IBaseAppPanel podla zadaneho ID</summary>
        /// <param name="pID">ID</param>
        /// <param name="pTabPosition">pozicia zalozky</param>
        /// <returns>prislusny objekt alebo null</returns>
        public static IBaseAppPanel CreateAppPanel(int pID, int pTabPosition)
        {
            switch (pID)
            {
                case IBaseAppPanel.APP_PANEL_ID_FILE_EXPLORER:
                    {
                        IFileExplorerPanel P = new IFileExplorerPanel(); // Vytvorim
                        P.TabPosition = pTabPosition;
                        return P; // Vratim obvekt
                    }
                default: return null;
            }
        }
    }
}
